package cn.yuanqiao.archive.danganzhengli.utils.impl;

import cn.yuanqiao.archive.danganzhengli.utils.OfdAgent;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import com.suwell.ofd.custom.agent.HTTPAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjj
 * @date 2023/8/8
 */
@Component
@ConditionalOnProperty(value= "YQ.OFD.VERSION" ,havingValue = "SUWELL" )
public class SuwellAgent implements OfdAgent {
    protected static final Logger logger = LoggerFactory.getLogger(SuwellAgent.class);
    @Value("${HD_SUWELL_AGENT_URL}")
    private String HD_SUWELL_AGENT_URL;
    /**
     * html转OFD
     * @param files
     * @param outPath
     * @throws Exception
     */
    public void htmlToOFD(List<File> files,String outPath) throws Exception {
        logger.info("SuwellAgent:htmlToOFD=outPath:"+outPath);
        HTTPAgent httpAgent = new HTTPAgent(HD_SUWELL_AGENT_URL);
        FileOutputStream out = null;
        try {
            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            List<String> f = new ArrayList<String>();

            for(File file : files) {
                String fPath = file.getPath();
                System.out.println(fPath);
                fPath = fPath.replace("\\", "/");
                f.add("file:///"+fPath);
            }
            out = new FileOutputStream(outPath);
            httpAgent.htmlToOFD(f,out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                httpAgent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(out!=null){
                out.close();
            }
        }
    }

    public void htmlToOFD1(List<File> files,String outPath) {
        logger.info("SuwellAgent:htmlToOFD=outPath:"+outPath);
        HTTPAgent httpAgent = new HTTPAgent(HD_SUWELL_AGENT_URL);
        FileOutputStream out = null;
        try {
            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            out = new FileOutputStream(outPath);
            httpAgent.officesToOFD(files,out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(httpAgent!=null){
                try {
                    httpAgent.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void officesToOfd(List<File> fileList, String outPath) throws Exception {
        if(StringUtils.isEmpty(HD_SUWELL_AGENT_URL)){
            throw new ServiceException("HD_SUWELL_AGENT_URL未找到配置");
        }
        HTTPAgent httpAgent = new HTTPAgent(HD_SUWELL_AGENT_URL);
        FileOutputStream out = null;
        try {
            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            out = new FileOutputStream(outPath);
            httpAgent.officesToOFD(fileList, out);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                httpAgent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(out!=null){
                out.close();
            }
        }
    }

    @Override
    public void imageToOFD(List<File> filesList, String outPath) throws Exception {
        logger.info("SuwellAgent:imageToOFD=outPath:"+outPath);
        if(StringUtils.isEmpty(HD_SUWELL_AGENT_URL)){
            throw new ServiceException("HD_SUWELL_AGENT_URL未找到配置");
        }
        HTTPAgent httpAgent = new HTTPAgent(HD_SUWELL_AGENT_URL);
        FileOutputStream out = null;
        try {
            if(!new File(outPath).getParentFile().exists()) {
                new File(outPath).getParentFile().mkdirs();
            }
            //DPI 350合适点
            out = new FileOutputStream(outPath);
            httpAgent.imagesToOFD(filesList,out,350);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                httpAgent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(out!=null){
                out.close();
            }
        }
    }
}
